class PM {  
    constructor() {  
        this.request = {  
            url: {  
                query: []
            },
            headers: [], 
            method: 'GET', // 可以根据需要设置请求方法，默认为 GET  
        }; 

        // 临时变量
        this.variables = new Map();
        this.response = {};
    }  
    
    // 重置请求参数
    newRequest(){
		this.request = {  
            url: {  
                query: []
            },
            headers: [],  
            method: 'GET', // 可以根据需要设置请求方法，默认为 GET  
        };
		 this.response = {};
	}

    // 添加查询参数  
    addQueryParam(key, value) {  
        this.request.url.query.push({ key, value });  
    }  
    // 添加Header参数  
    addHeadersParam(key, value) {  
        this.request.headers.push({ key, value });  
    }  

    // 模拟输出日志  
    log(...messages) {  
        console.log(...messages);  
    }  
    
    // 构建请求的完整URL  
    getFullUrl(baseUrl) {  
        const queryString = this.request.url.query  
            .map(param => `${encodeURIComponent(param.key)}=${encodeURIComponent(param.value)}`)  
            .join('&');  
        return `${baseUrl}?${queryString}`;  
    }  

    // 构建请求的完整URL  
    getQueryString() {  
        const queryString = this.request.url.query  
            .map(param => `${encodeURIComponent(param.key)}=${encodeURIComponent(param.value)}`)  
            .join('&');  
        return `${queryString}`;  
    }  
    
    // 获取headers参数
    getHeadersParam(){
		// 构建请求头对象  
        const headers = {};  
        this.request.headers.forEach(header => {  
            headers[header.key] = header.value;  
        });  
        return headers;
	}
    

    // 发送请求，支持回调函数  
    sendRequest(url, callback) {  
        const fullUrl = this.getFullUrl(url);  
        
        // 构建请求头对象  
        const headers = {};  
        this.request.headers.forEach(header => {  
            headers[header.key] = header.value;  
        });  
        
        var body={
					"url":fullUrl,
					"body":"",
					"query_para":"",
					"header":headers,
					"method":"POST",
					"content_type":"application/json"
			};
        
        $.ajax({
            type: 'post',
            url: _path+apiActionKey+'/sendRequest',
            data:JSON.stringify(body),
            dataType:'json',
            contentType:"application/json",
            timeout:30000, //设置超时的时间30s
	        complete: function(xhr,data){
				var contentType=xhr.getResponseHeader("content-type");
				if(contentType&&contentType.indexOf("application/json")!=-1){
		        	var resJson={};
		            try{
		            	resJson=JSON.parse(xhr.responseText);
		            	callback(resJson); // 调用回调函数  
		            }catch(e){
		                callback(xhr.responseText); // 调用回调函数  
		            }			       	
		        }else{
		        	callback(xhr.responseText); // 调用回调函数  
		        }
	        }  
	    });  

    }  
}  

// 使用示例  
const pm = new PM();  
//const apiKey = 'your_api_key';  
//const apiSecret = 'your_api_secret';  
//const timestamp = Math.floor(Date.now() / 1000);  
//const crypto = require('crypto-js'); // 记得安装 crypto-js 库  

// 生成签名  
//const sign = crypto.MD5(apiKey + apiSecret + timestamp).toString();  

// 添加查询参数  
//pm.addQueryParam('api_key', apiKey);  
//pm.addQueryParam('timestamp', timestamp.toString());  
//console.log(pm.request.url.query)
// 1、使用 eval() 函数  
//const codeToExecute = "pm.addQueryParam('api_key', apiKey);pm.addQueryParam('timestamp', timestamp.toString());";  
//eval(codeToExecute);  

// 2、使用 new Function() 方法  
//const dynamicFunction = new Function("console.log('Hello from new Function!');");  
//dynamicFunction(); 
//console.log(ad.getQueryString())
//pm.addQueryParam('sign', sign);  

// 打印生成的签名  
//pm.log('Generated Sign:', sign);  


// 发送请求并处理回调  
//pm.sendRequest('https://www.api.com/xxx', function(response) {   
//        console.log('Response Data:', jsonData);  
//});  
